package com.hsck.order.kds.main.fragment.order.batch

import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import androidx.recyclerview.widget.GridLayoutManager
import com.hsck.order.kds.R
import com.hsck.order.kds.base.isnoam.log.XFrame
import com.hsck.order.kds.base.isnoam.log.XLog
import com.hsck.order.kds.base.isnoam.view.BaseActivity
import com.hsck.order.kds.base.isnoam.view.SpacesItemDecoration
import com.hsck.order.kds.base.utils.GsonUtils
import com.hsck.order.kds.base.utils.UIHelper
import com.hsck.order.kds.databinding.ActivityDynamicBatchOrderingBinding
import com.hsck.order.kds.main.fragment.standard.bean.DishManager

/**
 * 订单动态-批量划菜
 */
class DynamicBatchOrderingActivity : BaseActivity<ActivityDynamicBatchOrderingBinding>() {
    override fun inflateBinding(inflater: LayoutInflater): ActivityDynamicBatchOrderingBinding {
        return ActivityDynamicBatchOrderingBinding.inflate(inflater)
    }

    private var dynamicBatchOrderingAdapter: DynamicBatchOrderingAdapter? = null

    override fun initData() {
        super.initData()
        initRecyclerView()
        dynamicBatchOrderingAdapter?.setListData(DishManager().getDishList())
        setSelectCount(0)
    }

    override fun initListener() {
        super.initListener()
        binding.apply {
            viewBack.setOnClickListener { finish() }
            tvSelectAll.setOnClickListener {
                dynamicBatchOrderingAdapter?.toggleItemSelected()
            }
        }
    }

    private fun setSelectCount(selectedCount: Int) {
        val message = String.format(XFrame.getString(R.string.select_all), selectedCount)
        val start = message.indexOf(selectedCount.toString())
        val end = start + selectedCount.toString().length
        binding.tvSelectAll.text = UIHelper.setTextViewContentStyle(
            message,
            null,
            ForegroundColorSpan(XFrame.getColor(R.color.color_007AFF)),
            start,
            end
        )
    }

    private fun initRecyclerView() {
        dynamicBatchOrderingAdapter = DynamicBatchOrderingAdapter(this)
        val gridLayoutManager = GridLayoutManager(this, 4)
        binding.recyclerView.setItemViewCacheSize(20) // 提高缓存的视图数量
        binding.recyclerView.setHasFixedSize(true)
        binding.recyclerView.isNestedScrollingEnabled = true
        binding.recyclerView.layoutManager = gridLayoutManager
        binding.recyclerView.adapter = dynamicBatchOrderingAdapter
        val spacingInPixels = resources.getDimensionPixelSize(R.dimen.four_column_spacing)
        binding.recyclerView.addItemDecoration(
            SpacesItemDecoration(
                spacingInPixels, true
            )
        )
        dynamicBatchOrderingAdapter!!.setDishOperationListener(object :
            DynamicBatchOrderingAdapter.DishOperationListener {
            override fun onDishOperationSelectedAction() {
                val selectedList = dynamicBatchOrderingAdapter!!.getSelectedList()
                setSelectCount(selectedList.size)
                binding.selectedAll = selectedList.size == dynamicBatchOrderingAdapter!!.itemCount
                XLog.i(GsonUtils.o2J(selectedList))
            }
        })
        dynamicBatchOrderingAdapter!!.setListData(DishManager().getDishList())
    }
}